package com.emiage.um;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.tencent.tauth.AuthActivity;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.editorpage.ShareActivity;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

	private Button mShared;
	private Button mLogin;
	private Button mLoginOut;
	private UMShareAPI mShareAPI;
	private UMAuthListener umAuthListener = new UMAuthListener() {
		@Override
		public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
			Toast.makeText( getApplicationContext(), "授权 succeed", Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(getApplicationContext(), OneActivity.class);
			startActivity(intent);
		}

		@Override
		public void onError(SHARE_MEDIA platform, int action, Throwable t) {
			Toast.makeText( getApplicationContext(), "授权 fail", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onCancel(SHARE_MEDIA platform, int action) {
			Toast.makeText( getApplicationContext(), "授权 cancel", Toast.LENGTH_SHORT).show();
		}
	};
	final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]
			{
					SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA,
					SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,SHARE_MEDIA.DOUBAN
			};
	UMShareListener umShareListener = new UMShareListener() {
		@Override
		public void onResult(SHARE_MEDIA share_media) {
			Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onError(SHARE_MEDIA share_media, Throwable throwable) {
			Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onCancel(SHARE_MEDIA share_media) {
			Toast.makeText(MainActivity.this, "onCancel", Toast.LENGTH_SHORT).show();
		}
	};
	private UMImage umImage;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mShared = (Button) findViewById(R.id.shared);
		mLogin = (Button) findViewById(R.id.login);
		mLoginOut = (Button) findViewById(R.id.loginout);
		mShared.setOnClickListener(this);
		mLogin.setOnClickListener(this);
		mLoginOut.setOnClickListener(this);

		mShareAPI = UMShareAPI.get(this);

		umImage = new UMImage(this, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
//		SHARE_MEDIA platform = SHARE_MEDIA.SINA;
		SHARE_MEDIA platform = SHARE_MEDIA.QQ;
//		SHARE_MEDIA platform = SHARE_MEDIA.WEIXIN;

		switch (id) {
			case R.id.shared://分享

				new ShareAction(this).setDisplayList( displaylist )
						.withText( "呵呵" )
						.withTitle("title")
						.withTargetUrl("http://www.baidu.com")
						.withMedia(umImage)
						.setListenerList(umShareListener)
//						.addButton("umeng_sharebutton_custom","SHARE_MEDIA.QQ","info_icon_1","info_icon_1")
//						.setShareboardclickCallback(new ShareBoardlistener() {
//							@Override
//							public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
//								if (snsPlatform.mShowWord.equals("umeng_sharebutton_custom")){
//									Toast.makeText(MainActivity.this,"自定义按钮",Toast.LENGTH_LONG).show();
//								}else {
//									new ShareAction(MainActivity.this).withText("来自友盟自定义分享面板")
//											.setPlatform(share_media)
//											.setCallback(umShareListener)
//											.share();
//								}
//							}
//						})
						.open();
//				new ShareAction(this).setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE)
//						.addButton("umeng_sharebutton_custom","umeng_sharebutton_custom","info_icon_1","info_icon_1")
//						.setShareboardclickCallback(new ShareBoardlistener() {
//							@Override
//							public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
//								if (snsPlatform.mShowWord.equals("umeng_sharebutton_custom")){
//									Toast.makeText(MainActivity.this,"自定义按钮",Toast.LENGTH_LONG).show();
//								}else {
//									new ShareAction(MainActivity.this).withText("来自友盟自定义分享面板")
//											.setPlatform(share_media)
//											.setCallback(umShareListener)
//											.share();
//								}
//							}
//						}).open();
//				break;
			case R.id.login://授权
				mShareAPI.doOauthVerify(this, platform, umAuthListener);
				break;
			case R.id.loginout://取消授权
				mShareAPI.deleteOauth(this, platform, new UMAuthListener() {
					@Override
					public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {

					}

					@Override
					public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

					}

					@Override
					public void onCancel(SHARE_MEDIA share_media, int i) {

					}
				});
				break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		mShareAPI.onActivityResult(requestCode, resultCode, data);
		UMShareAPI.get( this ).onActivityResult( requestCode, resultCode, data);
	}
}
